import req from '@/http/reqdata'
let {reqgetDetaildata} = req

export default{
    namespaced:true, 
    state:{
        detaildata:{},
        comsaleAttrarr:[],
        // 记录默认选中的下标
        defaultindex:[]
    },
    mutations:{
          setDetaildata(state,value){
             state.detaildata = value.data
            //  console.log(state.detaildata);
          },
          setisDefault(state,value){
              let {data,index} = value
            state.detaildata.skuInfo.skuImageList.forEach(item => {
                item.isDefault = '0'
              })
              // 2.2 当前被单击的小图片要设置成高亮
            //   data.isDefault = '1'
            state.detaildata.skuInfo.skuImageList[index].isDefault = '1'
              state.detaildata.skuInfo.skuDefaultImg = data.imgUrl
          },
          rmcomsaleAttrarr(state,index){
            state.comsaleAttrarr.splice(index,1,state.detaildata.spuSaleAttrList[index].spuSaleAttrValueList[state.defaultindex[index]])  
            // console.log(state.comsaleAttrarr);
            
            state.detaildata.spuSaleAttrList[index].spuSaleAttrValueList.forEach(item => {
                item.isChecked = '0'
            });
            state.detaildata.spuSaleAttrList[index].spuSaleAttrValueList[state.defaultindex[index]].isChecked = '1'
          },
          setcomsaleAttrarr(state){
            // console.log(state.detaildata.spuSaleAttrList);
                let newdata = state.detaildata.spuSaleAttrList.map(item =>{
                    return (item.spuSaleAttrValueList.find((item1,index)=>{
                        // state.defaultindex.push(index)
                        return item1.isChecked === '1'
                    }))
                }) 

                let newindex = state.detaildata.spuSaleAttrList.map(item =>{
                    return (item.spuSaleAttrValueList.findIndex((item1,index)=>{
                        // state.defaultindex.push(index)
                        return item1.isChecked === '1'
                    }))
                }) 

                state.defaultindex = newindex
                state.comsaleAttrarr = newdata
                // console.log(state.defaultindex);
                // console.log(state.comsaleAttrarr);
          },
          onsaleAttr(state,obj){
            let {data,num,num1} = obj
            

            // 选中数据
            // state.comsaleAttrarr[num]=data
            state.comsaleAttrarr.splice(num,1,data)
            // 排他样式
            state.detaildata.spuSaleAttrList[num].spuSaleAttrValueList.forEach(item => {
                item.isChecked = '0'
            });
            state.detaildata.spuSaleAttrList[num].spuSaleAttrValueList[num1].isChecked = '1'
          }

    },
    actions:{
        async getDetaildata(context,value){
             let newdata = await reqgetDetaildata(value)
             context.commit('setDetaildata',newdata)
        },
        
    },
    getters:{
        categoryView(state){
           return state.detaildata.categoryView || {}
        },
        skuInfo(state){
            return state.detaildata.skuInfo || {}
        },
        spuSaleAttrList(state){
            return state.detaildata.spuSaleAttrList || []
        },
    }
}